using System;
using System.Collections.Generic;
using System.Text;

namespace GuerillaProgrammer.Components
{
    public class StringProcessor
    {
        #region Private member fields
        private INameTable _ClientNameTable;
        #endregion

        #region Constructors and cleanup methods
        private StringProcessor()
        {
            //can't do this!
        }
        public StringProcessor(INameTable clientNameTable)
        {
            _ClientNameTable = clientNameTable;
        }
        #endregion

        #region Public interface
        public string SubstituteStringValues(string sourceString)
        {
            string updatedString = sourceString;
            int cursor = 0;
            bool done = false;

            while (!done)
            {
                cursor = updatedString.IndexOf("${", cursor);
                if (-1 == cursor)
                {
                    done = true;
                    break;
                }
                else
                {
                    int firstIndex = cursor + 2;
                    cursor = updatedString.IndexOf("}", cursor);
                    int length = cursor - firstIndex;
                    string ntValue = updatedString.Substring(firstIndex, length);
                    string replacementString = _ClientNameTable.GetValue(ntValue);

                    updatedString = updatedString.Replace("${" + ntValue + "}", replacementString);
                    cursor = 0;
                }
            }

            return updatedString;
        }
        #endregion
    }
}
